package com.muyi.controller.user;

import com.alibaba.fastjson.JSON;
import com.muyi.pojo.entity.User;
import com.muyi.service.UserService;
import com.muyi.service.impl.UserServiceImpl;
import com.muyi.pojo.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletGetUserBySearch", value = "/getUserBySearch")
public class ServletGetUserBySearch extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取请求参数
        String nickname = request.getParameter("nickname");
        Object status = request.getParameter("status");
        String gender = request.getParameter("gender");
        String graduation_year = request.getParameter("graduation_year");

        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //调用sercice层方法
        User user = new User();
        user.setNickname(nickname);
        user.setStatus(status);
        user.setGender(gender);
        user.setGraduation_year(graduation_year);
        //将执行结果返回给前端页面
        Result result = null;
        try {
            result = userService.getUserBySearch(user, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.getWriter().write(JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd"));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
